﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;

namespace CatalogImporter.Utils
{
    /// <summary>
    /// The product enumerator
    /// </summary>
    public sealed class ProductEnumerator : IEnumerator<Product>
    {
        #region Private members

        /// <summary>
        /// The file stream
        /// </summary>
        private StreamReader streamReader;

        /// <summary>
        /// The current product
        /// </summary>
        private Product current;

        #endregion

        #region Constructor

        public ProductEnumerator(String file)
        {
            this.streamReader = new StreamReader(File.OpenRead(file));

            // Discard the first line as it's just an header
            this.streamReader.ReadLine();
        }

        #endregion

        public Product Current
        {
            get { return this.current; }
        }

        object System.Collections.IEnumerator.Current
        {
            get { return this.current; }
        }

        public bool MoveNext()
        {
            if (this.streamReader.EndOfStream)
                return false;

            this.current = ParseProduct(this.streamReader.ReadLine());

            return true;
        }

        public void Reset()
        {
            throw new InvalidOperationException("Operation not supported by this enumerator");
        }

        public void Dispose()
        {
            if (this.streamReader != null)
            {
                this.streamReader.Close();
                this.streamReader.Dispose();
            }

            this.streamReader = null;
        }

        #region Private members

        private Product ParseProduct(String line)
        {
            Product product = null;

            try
            {
                String[] values = line.Split(new char[] { ';' });

                var formatProvider = CultureInfo.GetCultureInfo("en-GB");

                // Get the product attributes
                var colors = new List<ProductAttribute>(values[11].Split(new char[] { ',' }).Select(value => { return new ProductAttribute() { Value = value.Trim(), Price = new List<LocalizedItem<Double>>() }; }));
                var sizes = new List<ProductAttribute>(values[12].Split(new char[] { ',' }).Select(value => { return new ProductAttribute() { Value = value.Trim(), Price = new List<LocalizedItem<Double>>() }; }));

                // Colors price for it
                var colorsPrice = values[13].Split(new char[] { ',' });

                for (int i = 0; i < colorsPrice.Length; i++)
                    colors[i].Price.Add(new LocalizedItem<Double>() { Language = "it", Value = Double.Parse(colorsPrice[i], formatProvider) });

                // Sizes price for it
                var sizesPrice = values[14].Split(new char[] { ',' });

                for (int i = 0; i < sizesPrice.Length; i++)
                    sizes[i].Price.Add(new LocalizedItem<Double>() { Language = "it", Value = Double.Parse(sizesPrice[i], formatProvider) });

                // Colors price for en
                colorsPrice = values[15].Split(new char[] { ',' });

                for (int i = 0; i < colorsPrice.Length; i++)
                    colors[i].Price.Add(new LocalizedItem<Double>() { Language = "en", Value = Double.Parse(colorsPrice[i], formatProvider) });

                // Sizes price for en
                sizesPrice = values[16].Split(new char[] { ',' });

                for (int i = 0; i < sizesPrice.Length; i++)
                    sizes[i].Price.Add(new LocalizedItem<Double>() { Language = "en", Value = Double.Parse(sizesPrice[i], formatProvider) });

                product = new Product()
                {
                    Code = values[0],
                    Category = new List<LocalizedItem<String>>()
                {
                    new LocalizedItem<String> { Language = "it", Value = values[1] },
                    new LocalizedItem<String> { Language = "en", Value = values[2] }
                },
                    Name = new List<LocalizedItem<String>>()
                {
                    new LocalizedItem<String> { Language = "it", Value = values[3] },
                    new LocalizedItem<String> { Language = "en", Value = values[4] }
                },
                    Description = new List<LocalizedItem<String>>()
                {
                    new LocalizedItem<String> { Language = "it", Value = values[5] },
                    new LocalizedItem<String> { Language = "en", Value = values[6] }
                },
                    Shortcut = new List<LocalizedItem<String>>()
                {
                    new LocalizedItem<String> { Language = "it", Value = values[7] },
                    new LocalizedItem<String> { Language = "en", Value = values[8] }
                },
                    Price = new List<LocalizedItem<Double>>()
                {
                    new LocalizedItem<Double> { Language = "it", Value = Double.Parse(values[9], formatProvider) },
                    new LocalizedItem<Double> { Language = "en", Value = Double.Parse(values[10], formatProvider) }
                },
                    Colors = colors,
                    Sizes = sizes
                };
            }
            catch (Exception e)
            {
                System.Console.Write(e.Message);
            }

            return product;
        }

        #endregion
    }
}
